package cn.scy.util;

import cn.hutool.extra.spring.SpringUtil;
import cn.scy.business.service.GetTokenService;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 用户工具类
 *
 * @author scy
 */
public final class UserUtil {

    private UserUtil() {
    }

    /**
     * 获取当前用户id
     *
     * @return 用户id
     */
    public static String getCurrentUserId() {
        // 获取当前请求对象
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        // 使用现成的一个service来获取当前用户id
        GetTokenService service = SpringUtil.getBean(GetTokenService.class);
        return service.getUserIdByToken(request);
    }

}
